package client

import (
	"bgs/conf"
	"bgs/util"
	"context"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
	"time"
)

func UnaryPagerInterceptor(
	slog *zap.SugaredLogger,
	vars *conf.VarsSection,
	decider util.ClientInterceptorDecider,
) grpc.UnaryClientInterceptor {
	return func(
		ctx context.Context,
		method string,
		req interface{},
		reply interface{},
		cc *grpc.ClientConn,
		invoker grpc.UnaryInvoker,
		opts ...grpc.CallOption,
	) error {
		// Logic before invoking the invoker
		if decider(ctx, method) {
			ms := time.Now().Unix() * 1000
			tsStr := util.StringFromInt64(ms)
			ctx = metadata.AppendToOutgoingContext(ctx, "page_size", "20", "page_no", "1", "ts", tsStr)
			// ctx = NewMetadataContextWith(ctx, "page_size", "10")
		}
		// Calls the invoker to execute RPC
		err := invoker(ctx, method, req, reply, cc, opts...)

		// Logic after invoking the invoker
		return err
	}
}
